<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2017/12/1
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  String jsessionid = session.getId();
%>

<style>
  #talkA a{
    margin: 0 5px;
    line-height: 30px;
    color: #4FA5D0;
  }
</style>

<div class="panel">
  <div class="panel-heading header">
    <h3 class="panel-title" id="title">业务机会登记</h3>
  </div>

  <!-- BASIC FORM ELEMENTS -->
  <!--===================================================-->
  <form id="form_businessChance_input" class="form-horizontal">

    <input type="hidden" name="keyId" id="keyId" value="${businessChance.id}"/>
    <input type="hidden" name="curDutyId" id="curDutyId" value=""/>

    <div class="form-group">
      <label class="col-md-2 control-label">
        <a href="javascript:void(0);" onclick="fn_customerInfo_dialog_select()" style="color: #4FA5D0">客户名称</a>
        <i class="fa fa-asterisk txt-color-red"></i>
      </label>
      <div class="col-md-6">
        <input disabled type="text" id="customerInfo" class="form-control" placeholder="请输入客户名称" value="${businessChance.customerInfo.name}">
        <input type="hidden" id="customerInfoId" name="customerInfoId" class="form-control" value="${businessChance.customerInfo.id}">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">业务名称<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input type="text" name="name" id="name" class="form-control" placeholder="请输入业务名称" value="${businessChance.name}">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">业务成交可能性<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <%--<div class="input-group">--%>
          <input type="number" name="possibility" id="possibility" class="form-control" placeholder="请输入业务成交可能性" value="${businessChance.possibility}">
          <%--<span class="input-group-addon">%</span>--%>
        <%--</div>--%>
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">预计可签时间<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <input placeholder="请选择预计可签时间" id="signDatePlan" name="signDatePlan" class="form-control" type="text" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${businessChance.signDatePlan}" />">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">预计合同金额（万）<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <%--<div class="input-group">--%>
          <input type="number" name="moneyPlan" id="moneyPlan" class="form-control" placeholder="请输入预计合同金额" value="${businessChance.moneyPlan}">
          <%--<span class="input-group-addon">万</span>--%>
        </div>
      <%--</div>--%>
    </div>

    <div class="form-group">
      <label class="col-md-2 control-label">是否关联洽谈记录<i class="fa fa-asterisk txt-color-red"></i></label>
      <div class="col-md-6">
        <div class="radio">
          <input type="radio" name="isRelationTalk" id="isRelationTalk1" value="是" class="magic-radio" <c:if test="${businessChance.isRelationTalk=='是'}"> checked</c:if> >
          <label for="isRelationTalk1">是</label>
          <input type="radio" name="isRelationTalk" id="isRelationTalk2" value="否" class="magic-radio" <c:if test="${businessChance.isRelationTalk=='否'}"> checked</c:if> >
          <label for="isRelationTalk2">否</label>
        </div>
      </div>
    </div>

    <div class="form-group" id="talkDiv" <c:if test="${businessChance==null || businessChance.isRelationTalk=='否'}">style="display: none" </c:if>>
      <label class="col-md-2 control-label">
        <a href="javascript:void(0);" onclick="fn_businessTalks_dialog_select()" style="color: #4FA5D0">洽谈记录</a>
        <i class="fa fa-asterisk txt-color-red"></i>
      </label>
      <div class="col-md-6" id="talkA">
        <c:forEach items="${businessChance.businessTalkList}" var="talk" varStatus="status">
          <a href="javascript:void(0)" onclick="toTalk('${talk.id}')">${talk.talkTopic}</a>
        </c:forEach>
      </div>
      <input type="hidden" name="talkIds" id="talkIds" value="<c:forEach items="${businessChance.businessTalkList}" var="talk" varStatus="status">${talk.id}<c:if test="${!status.last}">,</c:if></c:forEach>" />
    </div>

  </form>
  <!--===================================================-->
  <!-- END BASIC FORM ELEMENTS -->

  <div class="panel-footer">
    <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
      <i class="fa fa-lg fa-mail-reply"></i> 返回
    </a>
    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存 </a>
    <c:if test="${businessChance==null || businessChance.processState=='Draft'}">
      <a class="btn btn-warning" id="btn-commit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交 </a>
    </c:if>
    <c:if test="${businessChance!=null && businessChance.processState=='Backed'}">
      <a class="btn btn-warning" id="btn-recommit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 再提交 </a>
    </c:if>
  </div>
</div>

<script>
  var index = "${index}";
  var todo = "${todo}";
  var remind = "${remind}";
  var record = "${record}";
  var draft =  "${draft}";

  $(function(){
    var pdata= {
      keyId:$("#keyId").val(),
      entityName:"com.fz.us.oa.core.entity.com.BusinessChance"
    };
    multiDuty(pdata);
    fn_businessChance_input_validate()
  });

  $('#signDatePlan').datepicker({
    format: 'yyyy-mm-dd',
    weekStart: 1,
    autoclose: true,
    todayBtn: 'linked',
    todayHighlight: true,
    language: 'zh-CN'
  });

  $(":radio[name='isRelationTalk']").click(function(){
    if($(this).val()=="是"){
      $("#talkDiv").show();
    }else{
      $("#talkDiv").hide();
    }
  });

  //选择客户信息
  function fn_customerInfo_dialog_select(){
    __open_dialog_select('客户信息', '<%=path%>/com/customerInfo/chooseCustomer', function (dialogRef) {
      var callback = function (data) {
        if (data) {
          dialogRef.close();
          $('#customerInfo').val(data.name);
          $('#customerInfoId').val(data.id);
        } else {
          dialogRef.enableButtons();
        }
      };
      fn_dialog_customerInfo_select(callback);
    });
  }

  //多选洽谈记录
  function fn_businessTalks_dialog_select() {
    __open_dialog_select('洽谈记录', '<%=path%>/com/businessChance/chooseTalk?sids='+$("#talkIds").val()+'&sTopics='+$("#talkTopics").val(), function (dialogRef) {
      var callback = function (data) {
        if (data) {
          dialogRef.close();
          var idsArr=data.ids.split(",");
          var topicsArr=data.topics.split(",");
          $('#talkIds').val(data.ids);

          $("#talkA").empty();
          for(var i=0;i<idsArr.length;i++){
            var newA='<a href="javascript:void(0)" onclick="toTalk(\''+idsArr[i]+'\')">'+topicsArr[i]+'</a>';
            $("#talkA").append(newA);
          }

        } else {
          dialogRef.enableButtons();
        }
      };
      fn_dialog_businessTalks_select(callback);
    });
  }

  function toTalk(id){
    __open_dialog_form_read("业务洽谈登记",'<%=path%>/com/businessTalk/read?keyId='+id);
  }

  // 表单数据校验
  function fn_businessChance_input_validate() {
    __init_validate('form_businessChance_input', {
      rules : {
        customerInfoId : {
          required : true
        },
        name : {
          required : true
        },
        possibility : {
          required : true,
          number : true,
          max : 100
        },
        signDatePlan : {
          required : true
        },
        moneyPlan:{
          required : true
        },
        isRelationTalk:{
          required : true
        },
        talkIds:{
          required : function(){
            if($(":checked[name='isRelationTalk']").val()=='是'){
              return true;
            }else{
              return false;
            }
          }
        }
      },
      messages : {
        customerInfoId : {
          required : "请选择客户名称"
        },
        name : {
          required : "请输入业务名称"
        },
        possibility : {
          required : "请输入业务成交可能性",
          number : '请输入数字',
          max : '最大不能超过100'
        },
        signDatePlan : {
          required : "请选择预计可签时间"
        },
        moneyPlan : {
          required : "请输入预计合同金额"
        },
        isRelationTalk : {
          required : "请选择是否关联洽谈记录"
        },
        talkIds : {
          required : "请选择洽谈记录"
        }
      }
    });
  }

  //保存
  $("#btn-save-common").click(function(){
    __confirm_dialog("提示","确定保存吗？",function(){
      __form_save("form_businessChance_input", "<%=path%>/com/businessChance/save",function(){
        $("#apply-re-common").trigger("click");
      })
    })
  });

  //提交
  $("#btn-commit-common,#btn-recommit-common").click(function(){
    if (!$('#form_businessChance_input').valid()) {
      return false;
    }
    __confirm_dialog("提示","确定提交吗？",function(){
      __form_save("form_businessChance_input", "<%=path%>/com/businessChance/commit",function(){
        $("#apply-re-common").trigger("click");
      })
    })
  });


  //返回视图
  $("#apply-re-common").click(function(){
    if(index==1){
      loadURL("menu/dashboard",$('#page-content'));
    }else if(todo==1){
      loadURL("menu/toDo",$('#page-content'));
    }else if(remind==1){
      loadURL("menu/remind",$('#page-content'));
    }else if(record==1){
      loadURL("menu/taskRecord?type=1",$('#page-content'));
    }else if(record==2){
      loadURL("menu/taskRecord?type=2",$('#page-content'));
    }else if(draft==1){
      loadURL("menu/draft",$('#page-content'));
    }else{
      returnToHashUrl()
    }
  });

</script>